/*
 * Copyright 2004 original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.extremecomponents.table.cell;

import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import org.extremecomponents.table.bean.Column;
import org.extremecomponents.table.cell.Cell;
import org.extremecomponents.table.core.TableConstants;
import org.extremecomponents.table.core.TableModel;
import org.extremecomponents.table.core.TableModelUtils;
import org.extremecomponents.table.view.html.TableActions;
import org.extremecomponents.util.ExtremeUtils;
import org.extremecomponents.util.HtmlBuilder;

/**
 * A filter cell that is a droplist
 * 
 * @author Jeff Johnston
 */
public class FilterDroplistTextCell extends org.extremecomponents.table.cell.FilterDroplistCell implements Cell {

	private String getValue(TableModel model, Column column, Object value){		
		if (column.isDate()) {
			Locale locale = model.getLocale();
            return ExtremeUtils.formatDate(column.getParse(), column.getFormat(), value, locale);
        }
		
		//------- change by sshwsfc get cell value ----------
		Cell cell = TableModelUtils.getCell(column);
		if(CellForFilter.class.isAssignableFrom(cell.getClass())){
			CellForFilter filerCell = (CellForFilter)cell;
			return filerCell.getParsedValue(model,column,value);
		}
		
		return value.toString();
	}
	
    protected String dropListHtml(TableModel model, Column column, List droplist) {
        HtmlBuilder html = new HtmlBuilder();

        html.td(2).close();

        html.newline();
        html.tabs(2);
        html.select().name(model.getTableHandler().prefixWithTableId() + TableConstants.FILTER + column.getAlias());

        StringBuffer onkeypress = new StringBuffer();
        onkeypress.append(new TableActions(model).getFilterAction());
        html.onchange(onkeypress.toString());

        html.close();

        html.newline();
        html.tabs(2);
        html.option().value("").close();
        html.optionEnd();        

        for (Iterator iter = droplist.iterator(); iter.hasNext();) {
            Object value = iter.next();
            
            html.newline();
            html.tabs(2);
            html.option().value("" + value);

            if (value.toString().equals(column.getValue().toString())) {
                html.selected();
            }
            
            String displayValue = getValue(model,column,value);
            
            html.close();
            html.append(displayValue);
            html.optionEnd();
        }

        html.newline();
        html.tabs(2);
        html.selectEnd();

        html.tdEnd();

        return html.toString();
    }
}
